package com.bdq.basic.domain.service.impl;

import com.bdq.basic.domain.service.ConfigDomainService;
import com.bdq.basic.infrastructure.persistence.mysql.pojo.BaseConfig;
import com.bdq.basic.infrastructure.persistence.mysql.service.BaseConfigService;
import com.bdq.common.constants.Constant;
import com.bdq.common.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author malei
 * @date 2024-05-02 18:11
 */
@Service
public class ConfigDomainServiceImpl implements ConfigDomainService {

    @Resource
    private BaseConfigService baseConfigService;

    @PostConstruct
    @Override
    public void loadConfig2Cache(){
        // 1 查询启用配置
        List<BaseConfig> configList = baseConfigService.listAllEnable();

        // 2 删除Redis缓存
        RedisUtil.delete(Constant.RedisKey.CONFIG);

        // 3 加载
        Map<String, String> configMap = new HashMap<>();
        for (BaseConfig config : configList) {
            configMap.put(config.getName(), config.getContent());
        }
        RedisUtil.hPutAll(Constant.RedisKey.CONFIG, configMap);
    }
}
